import {_decorator, Animation, AnimationClip, Component, SpriteFrame} from 'cc';
import {FSM_TYPE_ENUM, PARAMS_NAME_ENUM} from "../../Enums";
import State from "../../Base/State";
import {getInitParamNumber, getInitParamTrigger, StateMachine} from "../../Base/StateMachine";
import {IdleSubPlayerStateMachine, TurnLeftSubPlayerStateMachine, TurnRightSubPlayerStateMachine} from "./FourDirectionSubPlayerStateMachine";

const { ccclass, property } = _decorator;

const whiteList = ['turn']; // 播放完毕恢复idle动画



@ccclass('PlayerStateMachine')
export class PlayerStateMachine extends StateMachine {

    async init() {
        this.animationComponent = this.addComponent(Animation);

        this.initParams();
        this.initStateMachines();
        this.initAnimationEvent();

        await Promise.all(this.waitingList);
    }

    initAnimationEvent() {
        this.animationComponent.on(Animation.EventType.FINISHED, () => {
            if(whiteList.some(o => this.animationComponent.defaultClip.name.includes(o))){
                this.setParam(PARAMS_NAME_ENUM.IDLE, true);
            }
        });
    }
    initParams() {
        this.params.set(PARAMS_NAME_ENUM.IDLE, getInitParamTrigger());
        this.params.set(PARAMS_NAME_ENUM.TURNLEFT, getInitParamTrigger());
        this.params.set(PARAMS_NAME_ENUM.TURNRIGHT, getInitParamTrigger());
        this.params.set(PARAMS_NAME_ENUM.DIRECTION, getInitParamNumber());
    }
    initStateMachines() {
        this.stateMachines.set(PARAMS_NAME_ENUM.IDLE, new IdleSubPlayerStateMachine(this));
        this.stateMachines.set(PARAMS_NAME_ENUM.TURNLEFT, new TurnLeftSubPlayerStateMachine(this));
        this.stateMachines.set(PARAMS_NAME_ENUM.TURNRIGHT, new TurnRightSubPlayerStateMachine(this));
        // this.stateMachines.set(PARAMS_NAME_ENUM.IDLE, new State(this, 'texture/player/idle/top', AnimationClip.WrapMode.Loop));
        // this.stateMachines.set(PARAMS_NAME_ENUM.TURNLEFT, new State(this, 'texture/player/turnleft/top'));
    }

    run() {
        if (this.currentState == null) {
            this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.IDLE);
        } else if(this.params.get(PARAMS_NAME_ENUM.IDLE).value) {
            this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.IDLE);
        } else if(this.params.get(PARAMS_NAME_ENUM.TURNLEFT).value) {
            this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.TURNLEFT);
        } else if(this.params.get(PARAMS_NAME_ENUM.TURNRIGHT).value) {
            this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.TURNRIGHT);
        } else {
            this.currentState = this.currentState;
        }


        // switch (this.currentState) {
        //     case this.stateMachines.get(PARAMS_NAME_ENUM.IDLE):
        //     case this.stateMachines.get(PARAMS_NAME_ENUM.TURNLEFT):
        //         if(this.params.get(PARAMS_NAME_ENUM.IDLE).value) {
        //             this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.IDLE);
        //         } else if(this.params.get(PARAMS_NAME_ENUM.TURNLEFT).value) {
        //             this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.TURNLEFT);
        //         } else {
        //             this.currentState = this.currentState;
        //         }
        //         break;
        //     default:
        //         console.log('@@default run()', this.currentState)
        //         this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.IDLE);
        // }
    }

}

